<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib uri="http://www.ccut.edu.cn/saturn" prefix="saturn"%>

<%@page import="java.util.*"%>
<%@page import="edu.ccut.saturn.component.*"%>

<%@ include file="/jsp/include/header/meta.jsp"%>
<%@ include file="/jsp/include/header/stylesheettag.jsp"%>
<%@ include file="/jsp/include/header/javascript.jsp"%>
<%@page import="edu.ccut.saturn.component.impl.mock.MockRequest"%>
<html>
<head>
<title>值信息</title>
</head>
<body>
<%	
	Object value = null;
	System.out.println("222222222222"+request.getParameter("context"));
	System.out.println("3333333333333333333"+session.getAttribute(request.getParameter("context")));
	if (request.getParameter("context") != null) {
		value = session.getAttribute(request.getParameter("context"));
	} else {
		String type = request.getParameter("type");
		String index = request.getParameter("index");
		
		if (index != null && !"".equals(index)) {
			List parameters = (List)session.getAttribute(type);
		
			if (parameters != null) {
				IParameter parameter = (IParameter)parameters.get(Integer.parseInt(index));
				value = parameter.getValue();
			}
			
		} else {
			value = type;
		}
	}
	
	if (value != null) {
		List valueList = null;
		String title = "值的内容";
		if (value instanceof List ) {
			valueList = (List)value;
			title = "List的内容";
		} else if (value instanceof Map) {
			valueList = new ArrayList(1);
			valueList.add(value);
			title = "Map的内容";
		} else if (value instanceof MockRequest) {
			valueList = new ArrayList();
			title = "Request的内容";
			HttpServletRequest httpServletRequest = (HttpServletRequest) value;
			
			Enumeration paraNames = httpServletRequest.getParameterNames();
			while (paraNames.hasMoreElements()) {
				String key = (String)paraNames.nextElement();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", key);
				oneValueMap.put("value", httpServletRequest.getParameter(key));
				oneValueMap.put("scope", "request.parameter");
				
				valueList.add(oneValueMap);
			}
			
			Enumeration names = httpServletRequest.getAttributeNames();
			while (names.hasMoreElements()) {
				String name = (String)names.nextElement();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", httpServletRequest.getAttribute(name));
				oneValueMap.put("scope", "request.attribute");
				
				valueList.add(oneValueMap);
			} 
			
			Enumeration sessionNames = httpServletRequest.getSession().getAttributeNames();
			while (sessionNames.hasMoreElements()) {
				String name = (String)sessionNames.nextElement();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", httpServletRequest.getSession().getAttribute(name));
				oneValueMap.put("scope", "session.attribute");
				
				valueList.add(oneValueMap);
			}
		} else if (value instanceof ILogicContext) {
		 System.out.println("aaa");
			valueList = new ArrayList();
			title = "Context的内容";
			ILogicContext context = (ILogicContext)value;
			Iterator logicIterator = context.getValueKeys(IParameterInfo.SCOPE_LOGIC).iterator();
			while (logicIterator.hasNext()) {
				String name = (String)logicIterator.next();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", context.getValue(name, IParameterInfo.SCOPE_LOGIC));
				oneValueMap.put("scope", "logic");
				
				valueList.add(oneValueMap);
			}
			
			Iterator requestIterator = context.getValueKeys(IParameterInfo.SCOPE_REQUEST).iterator();
			while (requestIterator.hasNext()) {
				String name = (String)requestIterator.next();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", context.getValue(name, IParameterInfo.SCOPE_REQUEST));
				oneValueMap.put("scope", "request");
				
				valueList.add(oneValueMap);
			}
			
			Iterator sessionIterator = context.getValueKeys(IParameterInfo.SCOPE_SESSION).iterator();
			while (sessionIterator.hasNext()) {
				String name = (String)sessionIterator.next();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", context.getValue(name, IParameterInfo.SCOPE_SESSION));
				oneValueMap.put("scope", "session");
				
				valueList.add(oneValueMap);
			}
			
			Iterator applicatoinIterator = context.getValueKeys(IParameterInfo.SCOPE_APPLICATOIN).iterator();
			while (applicatoinIterator.hasNext()) {
				String name = (String)applicatoinIterator.next();
				Map oneValueMap = new HashMap();
				oneValueMap.put("key", name);
				oneValueMap.put("value", context.getValue(name, IParameterInfo.SCOPE_APPLICATOIN));
				oneValueMap.put("scope", "application");
				
				valueList.add(oneValueMap);
			}
		}
		
		if (valueList != null && !valueList.isEmpty()) {
			Object oneValue = valueList.get(0);
			request.setAttribute("showValue", valueList);
			request.setAttribute("title", title);
			%>
<saturn:formBeans name="nonull" title="${title}" action="">
	<saturn:table id="value" value="${showValue}" item="var">
		<%			
			if (oneValue instanceof Map) {
				Map oneMap = (Map) oneValue;
				Set keySet = oneMap.keySet();

				Iterator iterator = keySet.iterator();
				
				while (iterator.hasNext()) {
					String key = (String)iterator.next();
				%>
		<saturn:tableColumn title="<%=key%>"><%=((Map)pageContext.getAttribute("var")).get(key)!=null?((Map)pageContext.getAttribute("var")).get(key).toString():null%></saturn:tableColumn>
		<%
				}
	 		} else {
	 			%>
		<saturn:tableColumn title="值"><%=((Map)pageContext.getAttribute("var")).toString()%></saturn:tableColumn>
		<%
	 		}
	%>
	</saturn:table>
</saturn:formBeans>
<%
		} else {
			out.print(value.toString());
		}
	}
%>
</body>
</html>